<?php
	if (isset($_GET['id_criteria']) && isset($_GET['id_mark']) && isset($_GET['com']) && isset($_GET['id_rating']) && isset($_GET['final_mark']))
	{
		require_once('Class/connexionDB.Class.php');
		
		$db 			= new connexionDB();
		$id_criteria 	= intval($_GET['id_criteria']);
		$id_mark 		= intval($_GET['id_mark']);
		$id_rating		= intval($_GET['id_rating']);
		$com			= $_GET['com'];
		$old_mark 		= doubleval($_GET['final_mark']);

		$error = $db->updateIdCriteria($id_criteria, $id_mark, $id_rating, $com);
		//<!-- UPDATE MARK -->
		$result = $db->getMarkAndCoeff($id_rating);
		$finalMark 		= 0;
		$coefficient 	= 0;
		$sumCoeff 		= 0;
		$setToZero 		= false;

		foreach ($result as $value) {
			if ($value['coefficient'] == NULL)
				$coefficient = 0;
			else
				$coefficient = $value['coefficient'];

			if ($value['set_to_zero'] == 1 && $value['mark'] == 0)
				$setToZero = true;
			$finalMark += $value['mark'] * $coefficient * 5;
			$sumCoeff += $value['coefficient'];
		}

		if ($sumCoeff != 1)
		{
			$coeff = 1 - $sumCoeff;
			$finalMark += 20 * $coeff * 5;
		}

		//if the criteria has the "set_to_zero" option 
		if ($setToZero == true)
		{
			$finalMark = 0;
		}

		$db->updateFinalMark($finalMark, $id_rating);
		$db->saveOldMark($old_mark, $id_rating);
		$db->lastEditDate($id_rating);
	}
?>
